package com.wang.spring.bean.a_type;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

public class BeanTypeAnnoApplication {

    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(BeanTypeAnnoConfiguration.class);
        // 这里如果没有调用 ctx.getBean(Toy.class) 那么 ToyFactoryBean 产生的bean就不会出来
        // 也就是说 ToyFactoryBean 虽然初始化了，但是并没有生产 Toy bean，即使 Child 已经出来了初始化了
        // 可以说 FactoryBean 生产的bean 是延迟加载的
        Toy bean = ctx.getBean(Toy.class);
        Toy bean1 = ctx.getBean(Toy.class);

        // bean == bean1 为true 可以看出FactoryBean产生的bean都是单例的
        System.out.println(bean == bean1);
//        System.out.println(bean);

        // 2、获取FactoryBean类
        Object toFactory = ctx.getBean("toyFactoryAAA");
        FactoryBean factoryBean = ctx.getBean(FactoryBean.class);
        System.out.println("toFactory==="+toFactory);
        System.out.println("factoryBean === "+factoryBean);

        Object toyFactoryAAA = ctx.getBean("&toyFactoryAAA");
        System.out.println("toyFactoryAAA =="+toyFactoryAAA);

//        Map<String, Toy> beansOfType = ctx.getBeansOfType(Toy.class);
//        beansOfType.forEach((s, toy) -> System.out.println("toy name = " + s + "," + toy.toString()));
    }
}
